package samples 
{
	import flash.events.MouseEvent;
	import marcel.debug.debug;
	import marcel.display.gui.buttons.SimpleTextButton;
	import marcel.display.View;
	import marcel.external.twitter.Twitter;
	import marcel.external.twitter.TwitterEvent;
	/**
	 * ...
	 * @author Alexandre Croiseaux
	 */
	public class TwitterSample extends View
	{
		public var twitter:Twitter;
		
		public function TwitterSample() 
		{
			var consumerKey:String = "15Iqlmjq0nccOyTjIruuQ";
			var consumerSecret:String = "V3aOFouAy70WguAQiNGb3WnODOEra5dugEo5gTBWZOY";
			var callbackURI:String = "http://localhost/framework_as3_v2/apps/site/callback_twitter.html";			
			var proxyURI:String = "http://prod-publiciscorporate.integra.fr/tests/flash/proxy.php";
			
			
			// there is no crossdomain.xml on the twitter api website, we need a server proxy
			// see PHP example proxy code at the bottom of this sample class
			// api calls will be sent to this proxy as an Base64 encoded string in the 'url' GET variable
			
			
			twitter = new Twitter(consumerKey, consumerSecret, callbackURI, proxyURI);
			twitter.addEventListener(TwitterEvent.CONNECT, onTwitterEvent);
			twitter.addEventListener(TwitterEvent.ERROR, onTwitterEvent);
			twitter.addEventListener(TwitterEvent.REQUEST_TOKEN, onTwitterEvent);
			twitter.addEventListener(TwitterEvent.SET_STATUS, onTwitterEvent);
			twitter.addEventListener(TwitterEvent.USER_DATA, onTwitterEvent);
			twitter.addEventListener(TwitterEvent.USER_TIMELINE, onTwitterEvent);
			
			// UI
			x = y = 200;
			orientation = ORIENTATION_VERTICAL;
			if (!twitter.hasAccessToken())
			{
				twitter.requestToken();
				addChildWithProps(new SimpleTextButton("authorize app access"), { data:"access", click:onClick } );
			}
			else showAPIButtons();
		}
		
		private function onTwitterEvent(e:TwitterEvent):void 
		{
			debug("***********" + e.type + "*************");
			debug(e.data);
			
			if (e.type == TwitterEvent.CONNECT) showAPIButtons();
		}
		
		private function showAPIButtons():void
		{
			addChildWithProps(new SimpleTextButton("set status"), { data:"setStatus", click:onClick } );
			addChildWithProps(new SimpleTextButton("get user data"), { data:"user", click:onClick } );
			addChildWithProps(new SimpleTextButton("get timeline"), { data:"timeline", click:onClick } );
		}
		
		private function onClick(e:MouseEvent):void 
		{
			switch (e.currentTarget.data)
			{
				case "access":
					twitter.authorize();
					break;
				case "setStatus":
					twitter.setStatus("test new status fff " + new Date().getTime());
					break;
				case "user":
					twitter.getUserData(187882684);
					break;
				case "timeline":
					twitter.getUserTimeline(187882684);
					break;
			}
		}
		
	}

}

//--------------------------------------------------------------------------
//
//  example proxy file in PHP
//
//--------------------------------------------------------------------------

/*
<?php
 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, base64_decode($_GET['url']));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// fwd headers
$headers = apache_request_headers();
if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// disable ssl verify
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

// allow 5 redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

if (!empty($_POST))
{
	$postvars = '';
	while ($element = current($_POST))
	{
		$postvars .= key($_POST).'='.$element.'&';
		next($_POST);
	}
	curl_setopt ($ch, CURLOPT_POST, TRUE);
	curl_setopt ($ch, CURLOPT_POSTFIELDS, $postvars);
}

curl_exec($ch);

?>
*/


